﻿using Cudafy.Host;
using Lights.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lights
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            try
            {
                Global.OpenWorkspace("./Default");
                InitializeComponent();
            }
            catch (Exception e)
            {
                Profiling.ReportError(e);
            }
        }

        private void selection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(e.AddedItems.Count > 0)
                this.editingPage.SelectedItem = e.AddedItems[0] as Photo;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.library.PreExecution += PreExecution;
                this.library.ProgressChanged += ProgressChanged;
                this.library.PostExecution += PostExecution;

                StringBuilder sb = new StringBuilder("Core: ");
                if (CudafyHost.GetDeviceCount(Cudafy.eGPUType.OpenCL) > 0)
                    sb.Append("OpenCL");
                else
                    sb.AppendFormat("{0}x CPU ({1})", Environment.ProcessorCount, (Environment.Is64BitProcess ? "64bit" : "32bit"));
                this.txtInfo.Text = sb.ToString();

                Profiling.UpdateUserProfile();
                int i = 0;
                i = 5 / i;
            }
            catch (Exception exc)
            {
                Profiling.ReportError(exc);
            }
        }

        void PreExecution(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.infoPanel.IsEnabled = true;
                this.infoPanel.Visibility = System.Windows.Visibility.Visible;
                this.lblWork.Content = "Importazione";
            });
        }

        void PostExecution(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.prgBar.IsIndeterminate = false;
                this.infoPanel.Visibility = System.Windows.Visibility.Hidden;
                this.library.UpdateLayout();
                this.library.InvalidateVisual();
            });
        }

        private void ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
                {
                    this.prgBar.Value = e.ProgressPercentage;
                });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.library.CancelAsync();
            this.lblWork.Content = "Annullamento";
            this.prgBar.IsIndeterminate = true;
        }

    }
}
